package com.course.java.alipay.controller;

import com.course.java.alipay.service.PayService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 *  支付宝支付，控制器
 *
 * @author wangziyang
 * */
@RestController
@RequestMapping(value = "/pay")
@AllArgsConstructor
public class PayController {

    private PayService payService;
    @Autowired
    private AlipayCallbackController alipayCallbackController;

    /**
     *  下单支付11
     * */
    @PostMapping(value = "/confirm" , produces = {"text/html;charset=UTF-8"})
    public Object pay (@RequestParam(value = "total",required = false) BigDecimal total) throws Exception {
        System.out.println(total);
        return payService.pay(total);
    }

    /**
     *  支付成功的回调
     * */
    @PostMapping(value = "/fallback")
    public Object fallback (HttpServletRequest request) {
         String callback = alipayCallbackController.callback(request);
//        Map<String, String[]> parameterMap = request.getParameterMap();
//        for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
//            System.out.println(stringEntry);
//        }
        System.out.println(callback);
        return callback;
    }
}
